package com.wl.eb.controller;

import com.wf.captcha.utils.CaptchaUtil;
import com.wl.eb.module.EbUser;
import com.wl.eb.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("simple")
public class LoginController {
    @GetMapping("/login")
    public String loginPage(){
        return "login";
    }
    @GetMapping("/reg")
    public String loginReg(){
        return "register";
    }
    @Resource
    private LoginService loginService;
    @PostMapping
    public String login(EbUser ebUser,HttpServletRequest request){
        System.out.println("==LoginController的login方法，ebUser:"+ebUser);
        EbUser loginUser=loginService.login(ebUser);
        request.getSession().setAttribute("lgnUser",loginUser);
         return "redirect:index/index";
    }
    @PostMapping("register")
    public String register(EbUser ebUser,String verycode,HttpServletRequest request){
        System.out.println("==LoginController的reg方法，ebUser:"+ebUser);
        boolean result=loginService.register(ebUser);
        if(!verycode.equalsIgnoreCase((String)request.getSession().getAttribute("captcha"))){
            throw new RuntimeException("你是猪吧!!!");
        }
        if(!result){
            System.out.println("注册失败");
        }
        else {
            System.out.println("注册成功");
        }
        return "redirect:login";
    }
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

}
